package org.ktjy.quartz_demo.util;

import org.ktjy.quartz_demo.entity.JobBean;
import org.ktjy.quartz_demo.job.Quartz_SendEmail;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;

/**
 * @author Jane
 * @date 2024-12-11 17:16
 */

public class JobUtils{

    /***
     *创建定时任务
     * @param jobBean
     * @throws ClassNotFoundException
     */
    public static void createJob(Scheduler scheduler, JobBean jobBean) throws ClassNotFoundException {

//        Class<? extends Job> jobClass= (Class<? extends Job>) Class.forName(jobBean.getJobClass());
//
//        JobDetail jobDetail= JobBuilder
//                .newJob(jobClass)
//                .withIdentity(jobBean.getJobName())
//                .usingJobData("count",1)
//                .storeDurably() //是否支持持久化
//                .build();
//
//        Trigger trigger = TriggerBuilder.newTrigger()
//                .withIdentity(jobBean.getJobName()+"_trigger")
//                .forJob(jobDetail)
//                //.withSchedule(SimpleScheduleBuilder.simpleSchedule().withIntervalInSeconds(2).repeatForever())
//                .withSchedule(CronScheduleBuilder.cronSchedule(jobBean.getCronExpression()))
//                .build();
//
//        try {
//            Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
//            scheduler.scheduleJob(jobDetail,trigger);
////            scheduler.scheduleJob(jobDetailB,trigger);
////            scheduler.start();
//
////            try {
////                Thread.sleep(60000);
////            } catch (InterruptedException e) {
////                throw new RuntimeException(e);
////            }
////            scheduler.shutdown();
//        } catch (Exception e){
//            e.printStackTrace();
//        }
        Class<? extends Job> jobClass= null;
        JobDetail jobDetail = null;
        Trigger trigger=null;
        String croExp = "0/2 * * * * ?";

        try {
            jobClass=( Class<? extends Job>)Class.forName(jobBean.getJobClass());

            jobDetail= JobBuilder.newJob(jobClass)
                    .storeDurably()
                    .withIdentity(jobBean.getJobName())
                    .usingJobData("count", 1).build();

            trigger =  TriggerBuilder.newTrigger()
                    .withIdentity(jobBean.getJobName()+"_trigger")
                    .forJob(jobDetail)
                    .withSchedule(CronScheduleBuilder.cronSchedule(croExp))
                    .build();

            scheduler.scheduleJob(jobDetail,trigger);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (SchedulerException e) {
            throw new RuntimeException(e);
        }

    }

    /**
     * 暂停定时任务
     * @param scheduler
     * @param jobName
     */
    public static void pausueJob(Scheduler scheduler ,String jobName){
        JobKey jobKey =JobKey.jobKey(jobName);
        try {
            scheduler.pauseJob(jobKey);
        } catch (SchedulerException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 恢复定时任务
     * @param scheduler
     * @param jobName
     */
    public static void resumeJob(Scheduler scheduler ,String jobName){
        JobKey jobKey =JobKey.jobKey(jobName);
        try {
            scheduler.resumeJob(jobKey);
        } catch (SchedulerException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 删除定时任务
     * @param scheduler
     * @param jobName
     */
    public static void deleteJob(Scheduler scheduler ,String jobName){
        JobKey jobKey =JobKey.jobKey(jobName);
        try {
            scheduler.deleteJob(jobKey);
        } catch (SchedulerException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 启动一次定时任务
     * @param scheduler
     * @param jobName
     */
    public static void onceJob(Scheduler scheduler ,String jobName){
        JobKey jobKey =JobKey.jobKey(jobName);
        try {
            scheduler.triggerJob(jobKey);
        } catch (SchedulerException e) {
            throw new RuntimeException(e);
        }
    }


    /**
     * 修改定时任务
     * 不能改job类的类型,名称也不能
     * 能改的: 触发器的触发规则(启动要求)
     * @param scheduler
     * @param jobName
     */
//    public static void updateJob(Scheduler scheduler ,String jobName,String corn ){
    public static void updateJob(Scheduler scheduler ,JobBean jobBean){
        //拿到触发器
        //拿到启动的规则
        TriggerKey triggerKey = TriggerKey.triggerKey(jobBean.getJobName()+"_trigger");

        try {

            //拿到就的触发器
            CronTrigger old_trigger =  (CronTrigger)scheduler.getTrigger(triggerKey);
            String croExp = jobBean.getCronExpression(); //以及启动规则
            CronTrigger trigger = old_trigger.getTriggerBuilder()
                    .withSchedule(CronScheduleBuilder.cronSchedule(croExp))
                    .build(); //生成新的触发器

            scheduler.rescheduleJob(triggerKey,trigger); //启动
        } catch (SchedulerException e) {
            throw new RuntimeException(e);
        }
    }
}

